package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.ObjectUtil;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.core.service.BankPayLimitService;
import com.sztx.pay.center.dataaccess.dao.mysql.GatewaySwiftCodeBankIdMappingMysqlDAO;
import com.sztx.pay.center.rpc.api.domain.BankPayLimitInfo;
import com.sztx.pay.center.rpc.api.domain.BankPayLimitInfo.LimitInfo;
import com.sztx.pay.center.rpc.api.domain.ResponsePageModel;
import com.sztx.pay.center.rpc.api.service.BankPayLimitRpcService;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.dataaccess.mysql.config.PageQuery;
@Service
public class BankPayLimitRpcServiceImpl extends BaseServiceImpl implements BankPayLimitRpcService{
	@Autowired
	BankPayLimitService bankPayLimitService;
	@Autowired
	GatewaySwiftCodeBankIdMappingMysqlDAO gatewaySwiftCodeBankIdMappingMysqlDAO;
	
	@Override
	@SlaveQueryAnnotation
	public ResponsePageModel<BankPayLimitInfo> getPayLimitList(String bankId,
			String bankName, int pageNo, int pageSize) {
		ValidateUtil.gt(pageNo, 0, "页码不能小于1");
		ValidateUtil.gt(pageSize, 0, "每页大下不能小于1");
		PageQuery pageQuery = new PageQuery(pageNo, pageSize);
		ResponsePageModel<BankPayLimitInfo> rep= new ResponsePageModel<BankPayLimitInfo>();
		int payLimitCount = bankPayLimitService.getPayLimitCount(bankId, bankName);
		rep.setTotal(payLimitCount);
		if(payLimitCount > 0){
			List<com.sztx.pay.center.core.service.bean.rpc.BankPayLimitInfo> payLimit = bankPayLimitService.getPayLimitList(bankId, bankName, pageQuery );
			List<BankPayLimitInfo> payLimitList = new ArrayList<BankPayLimitInfo>();
//			for (com.sztx.pay.center.core.service.bean.rpc.BankPayLimitInfo bankPayLimitInfo : payLimit) {
//				BankPayLimitInfo tmp = new BankPayLimitInfo();
//				tmp = ObjectUtil.copyProperties(bankPayLimitInfo, tmp);
//				payLimitList.add(tmp);
//			}
			payLimitList = ObjectUtil.copyProperties(payLimit, payLimitList);
			rep.setResultLists(payLimitList);
		}
		return rep;
	}

	@Override
	public void updateLimitInfo(BankPayLimitInfo payLimitInfo) {
		ValidateUtil.isNotBlank(payLimitInfo.getBankId(), "银行ID不能为空");
		ValidateUtil.isNotBlank(payLimitInfo.getBankName(), "银行名称不能为空");
		List<LimitInfo> limitInfos = payLimitInfo.getLimitInfos();
		Byte payType = payLimitInfo.getPayType();
		for (LimitInfo limitInfo : limitInfos) {
			if(payType == 1){
				ValidateUtil.isNotBlank(limitInfo.getDesc(), "描述不能为空");
			}else if(payType == 2){
				if(limitInfo.getMonthLimit().intValue() != -1){
					ValidateUtil.gt(limitInfo.getMonthLimit().intValue(), 0, "月限额不能小于0");
				}
			}
			if(limitInfo.getSingleLimit().intValue() != -1){
				if(limitInfo.getSingleLimit().intValue() != -1){
					ValidateUtil.gt(limitInfo.getSingleLimit().intValue(), 0, "单笔限额不能小于0");
				}
			}
			if(limitInfo.getDayLimit().intValue() != -1){
				if(limitInfo.getDayLimit().intValue() != -1){
					ValidateUtil.gt(limitInfo.getDayLimit().intValue(), 0, "日限额不能小于0");
				}
			}
		}
		
		com.sztx.pay.center.core.service.bean.rpc.BankPayLimitInfo payLimit = new com.sztx.pay.center.core.service.bean.rpc.BankPayLimitInfo();
		payLimit = ObjectUtil.copyProperties(payLimitInfo, payLimit);
		bankPayLimitService.updateLimitInfo(payLimit ); 
	}

	@Override
	@SlaveQueryAnnotation
	public BankPayLimitInfo getPayLimitInfo(String bankId, int payType) {
		ValidateUtil.isBetween(payType, 0, 7, "支付类型错误");
		ValidateUtil.isNotBlank(bankId, "银行ID不能为空");
		com.sztx.pay.center.core.service.bean.rpc.BankPayLimitInfo payLimitInfo = bankPayLimitService.getPayLimitInfo(bankId, payType);
		BankPayLimitInfo payLimit = new BankPayLimitInfo();
		payLimit = ObjectUtil.copyProperties(payLimitInfo, payLimit);
		return payLimit;
		
	}

	@Override
	@SlaveQueryAnnotation
	public BankPayLimitInfo getPayLimitInfoBySwiftCode(String swiftCode,
			int payType) {
		ValidateUtil.isBetween(payType, 0, 7, "支付类型错误");
		ValidateUtil.isNotBlank(swiftCode, "银行swiftCode不能为空");
		String bankId = gatewaySwiftCodeBankIdMappingMysqlDAO.getBankIdBySwiftCode(swiftCode, payType);
		ValidateUtil.isNotBlank(bankId, "查询银行卡ID失败");
		com.sztx.pay.center.core.service.bean.rpc.BankPayLimitInfo payLimitInfo = bankPayLimitService.getPayLimitInfo(bankId, payType);
		BankPayLimitInfo payLimit = new BankPayLimitInfo();
		payLimit = ObjectUtil.copyProperties(payLimitInfo, payLimit);
		return payLimit;
	}
	
}
